import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o accountsList = [];
    @o firstRequest = false;


    will() {

    }

    addAdminMethod() {
        this.stores.AccountsDialog.fire('accountsDialog:show');
    }

    getAccountsList(loading) {
        loading && this.stores.Frame.showLoading();
        request({
            url: $$.api('getAccountsList')
        }).then(res => {
            this.stores.Frame.hideLoading();
            this.accountsList = res.data;
            this.firstRequest = true;
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    editClick(item) {
        this.stores.AccountsDialog.fire('accountsDialog:show', item);
    }

    modifyPwdClick(item) {
        this.stores.AccountsDialog.fire('accountsDialog:show', {...item, modifyPassword: true});
    }

    deleteClick(item) {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips: item.type === $$.SEM.ACCOUNT_ACTIVE_STATE ? '是否要冻结该管理员?' : '是否要解冻该管理员?',
            func: () => {
                if (item.type === $$.SEM.ACCOUNT_ACTIVE_STATE) {
                    this.stores.Frame.showLoading();
                    request({
                        url: $$.api('accountsDelete') + item.userId,
                        method: 'delete'
                    }).then(res => {
                        this.getAccountsList();
                        this.stores.Message.showMessage(1, res.message);
                    }).catch(err => {
                        this.stores.Frame.hideLoading();
                        this.stores.Message.showMessage(2, err.message);
                    });
                } else {
                    this.stores.Frame.showLoading();
                    request({
                        url: $$.api('accountsRestore') + item.userId,
                        method: 'put'
                    }).then(res => {
                        this.getAccountsList();
                        this.stores.Message.showMessage(1, res.message);
                    }).catch(err => {
                        this.stores.Frame.hideLoading();
                        this.stores.Message.showMessage(2, err.message);
                    });
                }
            }
        })
    }


    mount() {
        this.getAccountsList();
        this.stores.AccountsDialog.on('accountsDialog:update', () => {
            this.getAccountsList(true);
        });
    }

}

export default Store;